﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.CMS.Providers;
using System.Data;
using System.Collections;
using System.Web.UI;
using System.Configuration;

namespace Core.CMS
{
    public class PageInfo : IEnumerable 
    {
       //object _author = null;
        int _id = -1;
        int _parent = -1;
        string _title = "";
        string _desc = "";
        string _keywords = "";
        DateTime _created = DateTime.Now;
        DateTime _lastupdate = DateTime.Now;
        List<PageInfo> _children = null;

        public static PageInfo HomePage { get { return PageInfo.Get(int.Parse(ConfigurationManager.AppSettings["HomePage"])); } }

        public int ID
        {
            get { return _id; }
        }

        public int Children
        {
            get {
                if (_children == null)
                    GetChildren();

                return _children.Count;
            }
        }

        public int Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        public string Description
        {
            get { return _desc; }
            set { _desc = value; }
        }

        public string Keywords
        {
            get { return _keywords; }
            set { _keywords = value; }
        }

        public DateTime Created
        {
            get { return _created; }
        }

        public DateTime LastUpdated
        {
            get { return _lastupdate; }
        }

        public string Url { get { return this.ID.ToString(); } }

        /// <summary>
        /// For new pages create one using this
        /// </summary>
        public PageInfo() { }
        /// <summary>
        /// For existing pages the system will use this
        /// </summary>
        /// <param name="data"></param>
        internal PageInfo(IDataReader data)
        {
            _id = (int)data["id"];
            if (data["parent"] != DBNull.Value)
                _parent = (int)data["parent"];
            _title = (string)data["title"];
            _desc = (string)data["description"];
            _keywords = (string)data["keywords"];
            _created = (DateTime)data["created"];
            _lastupdate = (DateTime)data["LastUpdated"];
        }

        public void Save()
        {
            _id = AppFunctions.Content.SavePage(this);
        }

        public static PageInfo Get(int PageID) { return AppFunctions.Content.GetPage(PageID); }

        public string GetContent(string Name)
        {
            return AppFunctions.Content.GetContent(ID, Name);
        }

        public void SetContent(string Name, string NewContent)
        {
            AppFunctions.Content.SetContent(ID, Name, NewContent);
        }

        public IEnumerator GetEnumerator()
        {
            if (_children == null)
                _children = AppFunctions.Content.GetChildren(_id);

            return _children.GetEnumerator();
        }

        public PageInfo[] GetChildren()
        {
            if (_children == null)
                _children = AppFunctions.Content.GetChildren(_id);

            return _children.ToArray();
        }
    }
}
